package com.lv.service.adapter

import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lv.common.utils.Constants
import com.lv.common.utils.MyUtils.mContext
import com.lv.common.utils.TimeUtil
import com.lv.common.widget.video.YGXVideoNoBottomPlayer
import com.lv.service.R
import com.lv.service.bean.BookReceiptItemBean
import com.lv.service.bean.ViewTypeMediaRecipteBean
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.xuliwen.zoom.ZoomLayout

/**
 * 作者： ygx
 * 创建日期：2022/8/3 11:13 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class BookScanMediaTypeAdapter(data: MutableList<ViewTypeMediaRecipteBean>?) :
    BaseMultiItemQuickAdapter<ViewTypeMediaRecipteBean, BaseViewHolder>(data) {
    //    private var myVideo: YGXVideoTopPlayer? = null
    private val mVHMap = SparseArray<StandardGSYVideoPlayer>()

    init {
        //必须绑定type和layout的关系
        addItemType(ViewTypeMediaRecipteBean.PIC_TYPE, R.layout.item_image_full)
        addItemType(ViewTypeMediaRecipteBean.VEDIO_TYPE, R.layout.item_video_full)
    }

    override fun convert(helper: BaseViewHolder, item: ViewTypeMediaRecipteBean) {
        helper ?: return
        item ?: return
        when (helper.itemViewType) {
            ViewTypeMediaRecipteBean.PIC_TYPE -> {
                bindViewPic(helper, item.data)
            }
            ViewTypeMediaRecipteBean.VEDIO_TYPE -> {
                bindViewVideo(helper, item.data)
            }
            else -> print("其他布局")
        }
    }

    private fun bindViewPic(holder: BaseViewHolder, item: BookReceiptItemBean) {
        val zoom_layout = holder.getView<ZoomLayout>(R.id.zoom_layout)
        val tvTop = holder.getView<TextView>(R.id.tv_top)
        val tvCurrent = holder.getView<TextView>(R.id.tv_current)
        val tvBottom = holder.getView<TextView>(R.id.tv_bottom)
        val ivImg = holder.getView<ImageView>(R.id.iv_image)
        val images = item.images
        val positionChild = item.positionChild ?: 0
        val imageURL = images?.get(positionChild) ?: ""
        tvCurrent.text = "${positionChild + 1}/${images?.size ?: 0}"
        tvTop.text = "${item.book}   ${item.periods}期"
        val longNeedTimes = TimeUtil.longNeedTime(item.created)
        val prefecture = item.prefecture
        var name = item.cpname
        if (name.isNotEmpty()) {
            name = "$name\n"
        }
        if (!prefecture.isNullOrEmpty()) {
            tvBottom.text =
                "$name${item.provin}  ${item.city}  ${item.area}  ${prefecture}\n$longNeedTimes"
        } else {
            tvBottom.text =
                "$name${item.provin}  ${item.city}  ${item.area}\n$longNeedTimes"
        }
//        ivImg.load(item.image)
//        val layoutParams = zoom_layout.layoutParams as RecyclerView.LayoutParams
//        layoutParams.height = ScreenUtils.getScreenHeight()
//        zoom_layout.layoutParams = layoutParams
        val options = RequestOptions()
//                    .override(Target., Target.SIZE_ORIGINAL)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .format(DecodeFormat.PREFER_RGB_565)
//        val image = item.image
//        val width: Int = ScreenUtils.getScreenWidth()
//        var hight = 0
        Glide.with(context).load(imageURL).apply(options).into(ivImg)
//        if (image.contains("_WH_")) {
//            val a = image.split("_WH_")
//            val b = a[1].split("_".toRegex()).toTypedArray()
//            val c = b[1].split("\\.".toRegex()).toTypedArray()
//            if (c[0] != "0" && b[0] != "0") {
//                hight = width * c[0].toInt() / b[0].toInt()
//            } else {
//                hight = width
//            }
//        } else {
//            hight = width
//        }
//        val mHight = (ScreenUtils.getScreenHeight() - hight) / 2
//        val layoutTopParams = tvTop.layoutParams as RelativeLayout.LayoutParams
//        val layoutBottomParams =
//            tvBottom.layoutParams as RelativeLayout.LayoutParams
//        layoutTopParams.topMargin = mHight - SizeUtils.dp2px(25.0f)
//        tvTop.layoutParams = layoutTopParams
//        layoutBottomParams.bottomMargin = mHight - SizeUtils.dp2px(40.0f)
//        tvBottom.layoutParams = layoutBottomParams
        mVHMap.append(holder.layoutPosition, null)

//                 tvName.text = TimeUtil.longNeedTime(item.created)
//
////                val image = data?.images?.get(realPosition)
//                Glide.with( ivImg.context).load(data?.image)
//                    .override(SizeUtils.dp2px(360.0f), SizeUtils.dp2px(videoH))
//                    .error(com.lv.common.R.drawable.ic_syy_logo)
//                    .into( imageView)
//                mVHMap.append(layoutPosition, picHolder)

//        clParent.setOnClickListener {
//                    onViewClickListener?.onClick(layoutPosition, data)
//        }
    }

    private fun bindViewVideo(holder: BaseViewHolder, item: BookReceiptItemBean) {
        val clParent = holder.getView<ConstraintLayout>(R.id.cl_parent)
        val tvTop = holder.getView<TextView>(R.id.tv_top)
        val tvCurrent = holder.getView<TextView>(R.id.tv_current)
        val tvBottom = holder.getView<TextView>(R.id.tv_bottom)
        val positionChild = item.positionChild ?: 0
        val images = item.images
        val imageURL = images?.get(positionChild) ?: ""
        tvCurrent.text = "${positionChild + 1}/${images?.size ?: 0}"
        tvTop.text = "${item.book}   ${item.periods}期"
        val longNeedTimes = TimeUtil.longNeedTime(item.created)
        val prefecture = item.prefecture
        var name = item.cpname
        if (name.isNotEmpty()) {
            name = "$name\n"
        }
        if (!prefecture.isNullOrEmpty()) {
            tvBottom.text =
                "$name${item.provin}  ${item.city}  ${item.area}  ${prefecture}\n$longNeedTimes"
        } else {
            tvBottom.text =
                "$name${item.provin}  ${item.city}  ${item.area}\n$longNeedTimes"
        }
        val myVideo = holder.getView<YGXVideoNoBottomPlayer>(R.id.video_play)
        myVideo.setUpLazy(imageURL, true, mContext.cacheDir, null, "")
//        MediaUtils.getImageForVideo(context, imageURL) {
//            //获取到第一帧图片后再显示操作按钮
//            LogUtils.e("获取到了第一帧")
//            myVideo.loadCoverImage(it.absolutePath, 0)
//        }
        if (!imageURL.contains(Constants.FRIST_PIC)) {
            val fristPic = imageURL + Constants.FRIST_PIC
            myVideo.loadCoverImage(fristPic, 0)
        }
        myVideo.titleTextView?.visibility = View.GONE
        myVideo.backButton?.visibility = View.GONE
        myVideo.startButton?.visibility = View.VISIBLE
        myVideo.fullscreenButton?.visibility = View.GONE
        myVideo.setPlayResource(com.lv.common.R.drawable.icon_play_tag)
        myVideo.setShodowShow(false)
        myVideo.isLooping = true
//        myVideo?.startButton?.performClick()
        myVideo.isRotateViewAuto = false
        myVideo.isLockLand = true
        myVideo.isShowFullAnimation = true
        //音频焦点冲突时是否释放
        myVideo.isReleaseWhenLossAudio = true
        //禁止全屏
        myVideo.isAutoFullWithSize = true
        //禁止滑动
        myVideo.setIsTouchWiget(true)
        //静音
//        GSYVideoManager.instance().isNeedMute = true
        clParent.setOnClickListener { myVideo.fullscreenButton?.performClick() }
        mVHMap.append(holder.layoutPosition, myVideo)

    }

    fun getVHMap(): SparseArray<StandardGSYVideoPlayer> {
        return mVHMap
    }
//    //释放视频播放器
//    fun setVideoStop() {
//        myVideo?.release()
//    }
//
//    fun getMyvideo(): StandardGSYVideoPlayer? {
//        return myVideo
//    }
}
